package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.data.model.LegalTenderBalance;
import com.chain.BrotherExchange.utils.Androids;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 发布交易单
 */
public class PublishTransactionOrderViewModel extends AndroidViewModel {

    private int uid = Androids.LocalConfig.getUid();
    private String token = Androids.LocalConfig.getToken();

    private MutableLiveData<String> mUnitPriceLiveData;
    private MutableLiveData<Resource<BaseResponseModel>> mPublishOrderLiveData;
    private MutableLiveData<LegalTenderBalance.DataBean> mBalanceLiveData;

    public PublishTransactionOrderViewModel(@NonNull Application application) {
        super(application);
        mBalanceLiveData = new MutableLiveData<>();
        mUnitPriceLiveData = new MutableLiveData<>();
        mPublishOrderLiveData = new MutableLiveData<>();
    }

    /**
     * 单币种余额
     */
    public MutableLiveData<LegalTenderBalance.DataBean> singleCoinBalance(int tokenId) {
        DataRepository.get().legalTenderBalance(uid, token, tokenId, result -> {
            if (result.isSucceed()) {
                mBalanceLiveData.setValue(result.getData());
            }
        });
        return mBalanceLiveData;
    }

    /**
     * 发面交易
     */
    public MutableLiveData<Resource<BaseResponseModel>> publishOrder(
            int type, int tokenId, String tokenName, String price, String number, String premium,
            String acceptPrice, String minPrice, String maxPrice, boolean isVerify,
            List<String> paymentList, String remark) {
        Resources resources = getApplication().getResources();

        StringBuilder payment = new StringBuilder();
        if (TextUtils.isEmpty(number)) {
            mPublishOrderLiveData.setValue(Resource.error(resources.getString(R.string.hint_input_transaction_number)));
            return mPublishOrderLiveData;
        }

        if (TextUtils.isEmpty(minPrice)) {
            mPublishOrderLiveData.setValue(Resource.error(resources.getString(R.string.please_enter_single_minimum)));
            return mPublishOrderLiveData;
        }

        if (TextUtils.isEmpty(maxPrice)) {
            mPublishOrderLiveData.setValue(Resource.error(resources.getString(R.string.please_enter_single_maximum)));
            return mPublishOrderLiveData;
        }

        if (paymentList.isEmpty()) {
            mPublishOrderLiveData.setValue(Resource.error(resources.getString(R.string.text_please_select_payment_method)));
            return mPublishOrderLiveData;
        } else {
            for (String s : paymentList) {
                payment.append(s).append(",");
            }
            payment.delete(payment.length() - 1, payment.length());
        }

        if (TextUtils.isEmpty(premium)) {
            premium = "0";
        }

        if (TextUtils.isEmpty(acceptPrice)) {
            acceptPrice = "0";
        }

        mPublishOrderLiveData.setValue(Resource.loading());
        DataRepository.get().publishTransaction(
                uid, token, type, tokenId, tokenName, Double.parseDouble(price), Double.parseDouble(number),
                Double.parseDouble(premium), Double.parseDouble(acceptPrice), Double.parseDouble(minPrice),
                Double.parseDouble(maxPrice), isVerify, payment.toString(), remark, result -> {
                    if (result.isSucceed()) {
                        mPublishOrderLiveData.setValue(Resource.success(result.getMsg()));
                    } else {
                        mPublishOrderLiveData.setValue(Resource.error(result.getMsg()));
                    }
                });

        return mPublishOrderLiveData;
    }

    /**
     * 单价
     */
    public MutableLiveData<String> unitPrice(int tokenId) {
        DataRepository.get().legalTenderUnitPrice(tokenId, result -> {
            if (result.isSucceed()) {
                mUnitPriceLiveData.setValue(new DecimalFormat("########.########")
                        .format(result.getData().getMarket_price()));
            }
        });

        return mUnitPriceLiveData;
    }

    /**
     * 修改已发布交易单
     */
    public MutableLiveData<Resource<BaseResponseModel>> editPublishOrder(
            int id, String price, String number, String premium, String acceptPrice, String minPrice,
            String maxPrice, boolean isVerify, List<String> paymentList, String remark) {
        Resources resources = getApplication().getResources();
        StringBuilder payment = new StringBuilder();
        if (TextUtils.isEmpty(number)) {
            mPublishOrderLiveData.setValue(Resource.error(resources.getString(R.string.hint_input_transaction_number)));
            return mPublishOrderLiveData;
        }

        if (TextUtils.isEmpty(minPrice)) {
            mPublishOrderLiveData.setValue(Resource.error(resources.getString(R.string.please_enter_single_minimum)));
            return mPublishOrderLiveData;
        }

        if (TextUtils.isEmpty(maxPrice)) {
            mPublishOrderLiveData.setValue(Resource.error(resources.getString(R.string.please_enter_single_maximum)));
            return mPublishOrderLiveData;
        }

        if (paymentList.isEmpty()) {
            mPublishOrderLiveData.setValue(Resource.error(resources.getString(R.string.text_please_select_payment_method)));
            return mPublishOrderLiveData;
        } else {
            for (String s : paymentList) {
                payment.append(s).append(",");
            }
            payment.delete(payment.length() - 1, payment.length());
        }

        if (TextUtils.isEmpty(premium)) {
            premium = "0";
        }

        if (TextUtils.isEmpty(acceptPrice)) {
            acceptPrice = "0";
        }

        mPublishOrderLiveData.setValue(Resource.loading());
        DataRepository.get().editMyPublishTransactionOrder(
                uid, token, id, Double.parseDouble(price), Double.parseDouble(number),
                Double.parseDouble(premium), Double.parseDouble(acceptPrice), Double.parseDouble(minPrice),
                Double.parseDouble(maxPrice), isVerify, payment.toString(), remark, result -> {
                    if (result.isSucceed()) {
                        mPublishOrderLiveData.setValue(Resource.success(result.getMsg()));
                    } else {
                        mPublishOrderLiveData.setValue(Resource.error(result.getMsg()));
                    }
                });

        return mPublishOrderLiveData;
    }
}
